﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_BGM_Project : IDAL_BGM_Project
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<M_BGM_Project> GetFull(Nullable<int> bgm_year
            , Nullable<int> job_site_seq, Nullable<int> bgm_option_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_Project> infoList = new List<M_BGM_Project>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@approve_status", SystemInfo._ApproveRecordStatus);
                //dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);
                dbManager.AddParameters(1, "@bgm_year", bgm_year);
                dbManager.AddParameters(2, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(3, "@bgm_option_seq", bgm_option_seq);

                strSQL = "SELECT distinct rec.bgm_proj_seq, rec.bgm_proj_code"
                    + ", rec.bgm_proj_name, rec.bgm_proj_descr, rec.bgm_proj_date"
                    + ", rec.bgm_strat_list_seq, rec.job_site_seq, rec.bgm_year"

                    + ", strlist.bgm_strat_list_name"
                    + ", rec.record_status"
                    + " FROM bgm_project rec"
                    + " LEFT JOIN bgm_strategy_data_list strlist ON rec.bgm_strat_list_seq = strlist.bgm_strat_list_seq"
                    + (bgm_option_seq != null ?
                        " INNER JOIN bgm_project_option recop ON rec.bgm_proj_seq = recop.bgm_proj_seq"
                            + " AND recop.bgm_option_seq = @bgm_option_seq" : "")
                    + " WHERE rec.record_status = @approve_status"
                    + (bgm_year != null ? " AND rec.bgm_year = @bgm_year" : "")
                    + (job_site_seq != null ? " AND rec.job_site_seq = @job_site_seq" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_Project info = new M_BGM_Project();
                        info.bgm_proj_seq = Convert.ToInt32(drc[i]["bgm_proj_seq"]);
                        info.bgm_proj_code = drc[i]["bgm_proj_code"].ToString();
                        info.bgm_proj_name = drc[i]["bgm_proj_name"].ToString();
                        info.bgm_proj_descr = drc[i]["bgm_proj_descr"].ToString();
                        info.bgm_proj_date = cn.ConvertNullDateTime(drc[i]["bgm_proj_date"]);
                        info.bgm_strategy_list_seq = cn.ConvertNullInt(drc[i]["bgm_strat_list_seq"]);
                        info.job_site_seq = cn.ConvertNullInt(drc[i]["job_site_seq"]);
                        info.bgm_year = cn.ConvertNullInt(drc[i]["bgm_year"]);

                        M_BGM_StrategyDataList subInfo1 = new M_BGM_StrategyDataList();
                        subInfo1.bgm_strat_list_name = drc[i]["bgm_strat_list_name"].ToString();
                        info.m_bgm_subactivity = subInfo1;

                        info.record_status = drc[i]["record_status"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_Project> GetList(Nullable<int> bgm_year
            , Nullable<int> job_site_seq, Nullable<int> bgm_option_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_Project> infoList = new List<M_BGM_Project>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@approve_status", SystemInfo._ApproveRecordStatus);
                //dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);
                dbManager.AddParameters(1, "@bgm_year", bgm_year);
                dbManager.AddParameters(2, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(3, "@bgm_option_seq", bgm_option_seq);

                strSQL = "SELECT distinct rec.bgm_proj_seq, rec.bgm_proj_name"
                    + " FROM bgm_project rec"
                    + (bgm_option_seq != null ?
                        " INNER JOIN bgm_project_option recop ON rec.bgm_proj_seq = recop.bgm_proj_seq"
                            + " AND recop.bgm_option_seq = @bgm_option_seq" : "")
                    + " WHERE rec.record_status = @approve_status"
                    + (job_site_seq != null ? " AND rec.job_site_seq = @job_site_seq" : "")
                    + (bgm_year != null ? " AND rec.bgm_year = @bgm_year" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_Project info = new M_BGM_Project();
                        info.bgm_proj_seq = Convert.ToInt32(drc[i]["bgm_proj_seq"]);
                        info.bgm_proj_name = drc[i]["bgm_proj_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_Project> GetBudget(Nullable<int> bgm_proj_seq, Nullable<int> bgm_source_seq
            , Nullable<int> bgm_proj_oper_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_Project> infoList = new List<M_BGM_Project>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@approve_status", SystemInfo._ApproveRecordStatus);
                dbManager.AddParameters(1, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@receive_report_type", SystemInfo._ReceiveReportType);
                dbManager.AddParameters(3, "@bgm_proj_seq", bgm_proj_seq);
                dbManager.AddParameters(4, "@bgm_source_seq", bgm_source_seq);
                dbManager.AddParameters(5, "@bgm_proj_oper_seq", bgm_proj_oper_seq);

                strSQL = "SELECT rec.bgm_proj_seq, rec.bgm_proj_code, rec.bgm_proj_name"
                    + ", recexp.bgm_source_seq, s.bgm_source_name"
                    + ", exp.bgm_exp_grp_seq, expgrp.bgm_exp_grp_name"
                    + ", sum(coalesce(rpt1.cal_amount, 0)) as project_amount"
                    + ", sum(coalesce(rpt2.cal_amount, 0)) as remain_amount"

                    + " FROM bgm_project rec"
                    + " LEFT JOIN bgm_project_expense recexp on rec.bgm_proj_seq = recexp.bgm_proj_seq"
                    //================ Receive ===================
                    + " LEFT JOIN (SELECT pjrpt.bgm_proj_exp_seq, sum(coalesce(pjrpt.amount, 0)) as cal_amount"
                        + " FROM bgm_project_operation_expense pjrpt"
                        + " inner join bgm_project_operation pj on pjrpt.bgm_proj_oper_seq = pj.bgm_proj_oper_seq"
                        + " WHERE pjrpt.record_status not like @delete_status"
                        + " and pj.record_status = @approve_status"
                        + (bgm_proj_oper_seq != null ? " AND coalesce(pjrpt.bgm_proj_oper_seq, -1) = @bgm_proj_oper_seq" : "")
                        + " GROUP BY pjrpt.bgm_proj_exp_seq"
                        + ") rpt1 ON recexp.bgm_proj_exp_seq = rpt1.bgm_proj_exp_seq"
                    //================ Remain ===================
                    + " LEFT JOIN (SELECT pjrpt.bgm_proj_exp_seq, sum(coalesce(pjrpt.cal_amount, 0)) as cal_amount"
                        + " FROM bgm_project_report pjrpt"
                        + " LEFT JOIN bgm_project_operation_expense opexp ON pjrpt.bgm_proj_oper_exp_seq = opexp.bgm_proj_oper_exp_seq"
                        + " WHERE pjrpt.record_status not like @delete_status"
                        + (bgm_proj_oper_seq != null ? " AND coalesce(opexp.bgm_proj_oper_seq, -1) not like @bgm_proj_oper_seq" : "")
                        + " GROUP BY pjrpt.bgm_proj_exp_seq"
                        + ") rpt2 ON recexp.bgm_proj_exp_seq = rpt2.bgm_proj_exp_seq"
                    + " LEFT JOIN bgm_mas_source s ON recexp.bgm_source_seq = s.bgm_source_seq"
                    + " LEFT JOIN bgm_mas_expense exp ON recexp.bgm_exp_seq = exp.bgm_exp_seq"
                    + " LEFT JOIN bgm_mas_expense_group expgrp ON exp.bgm_exp_grp_seq = expgrp.bgm_exp_grp_seq"
                    + " WHERE rec.record_status = @approve_status"
                    + (bgm_proj_seq != null ? " AND rec.bgm_proj_seq = @bgm_proj_seq" : "")
                    + " GROUP BY rec.bgm_proj_seq, rec.bgm_proj_code, rec.bgm_proj_name"
                        + ", recexp.bgm_source_seq, s.bgm_source_name"
                        + ", exp.bgm_exp_grp_seq, expgrp.bgm_exp_grp_name"
                    + " ORDER BY recexp.bgm_source_seq, exp.bgm_exp_grp_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_Project info = new M_BGM_Project();
                        info.bgm_proj_seq = Convert.ToInt32(drc[i]["bgm_proj_seq"]);
                        info.bgm_proj_code = drc[i]["bgm_proj_code"].ToString();
                        info.bgm_proj_name = drc[i]["bgm_proj_name"].ToString();
                        info.bgm_source_seq = cn.ConvertNullInt(drc[i]["bgm_source_seq"]);
                        info.bgm_exp_grp_seq = cn.ConvertNullInt(drc[i]["bgm_exp_grp_seq"]);
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        info.bgm_exp_grp_name = drc[i]["bgm_exp_grp_name"].ToString();
                        info.project_amount = cn.ConvertNullDouble(drc[i]["project_amount"]);
                        info.remain_amount = cn.ConvertNullDouble(drc[i]["remain_amount"]);

                        //M_BGM_StrategyDataList subact = new M_BGM_StrategyDataList();
                        //subact.bgm_strat_list_name = drc[i]["bgm_sub_act_name"].ToString();
                        //info.m_bgm_subactivity = subact;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_BGM_Project GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_BGM_Project info = new M_BGM_Project();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT rec.bgm_proj_seq, rec.bgm_proj_code"
                    + ", rec.bgm_proj_name, rec.bgm_proj_descr, rec.bgm_proj_date"
                    + ", rec.bgm_strat_list_seq, rec.job_site_seq, rec.bgm_year"

                    + ", strlist1.bgm_strat_list_name as bgm_sub_act_name"
                    + ", strlist2.bgm_strat_list_name as bgm_act_name"
                    + ", strlist3.bgm_strat_list_name as bgm_out_put_name"
                    + ", rec.record_status"
                    + " FROM bgm_project rec"
                    + " LEFT JOIN bgm_strategy_data_mapping strmap1 ON rec.bgm_strat_list_seq = strmap1.bgm_strat_list_seq"
                    + " LEFT JOIN bgm_strategy_data_mapping strmap2 ON strmap1.upper_bgm_strat_data_map_seq = strmap2.bgm_strat_data_map_seq"
                    + " LEFT JOIN bgm_strategy_data_mapping strmap3 ON strmap2.upper_bgm_strat_data_map_seq = strmap3.bgm_strat_data_map_seq"

                    + " LEFT JOIN bgm_strategy_data_list strlist1 ON strmap1.bgm_strat_list_seq = strlist1.bgm_strat_list_seq"
                    + " LEFT JOIN bgm_strategy_data_list strlist2 ON strmap2.bgm_strat_list_seq = strlist2.bgm_strat_list_seq"
                    + " LEFT JOIN bgm_strategy_data_list strlist3 ON strmap3.bgm_strat_list_seq = strlist3.bgm_strat_list_seq"
                    + " WHERE rec.bgm_proj_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bgm_proj_seq = Convert.ToInt32(drc[0]["bgm_proj_seq"]);
                    info.bgm_proj_code = drc[0]["bgm_proj_code"].ToString();
                    info.bgm_proj_name = drc[0]["bgm_proj_name"].ToString();
                    info.bgm_proj_descr = drc[0]["bgm_proj_descr"].ToString();
                    info.bgm_proj_date = cn.ConvertNullDateTime(drc[0]["bgm_proj_date"]);
                    info.bgm_strategy_list_seq = cn.ConvertNullInt(drc[0]["bgm_strat_list_seq"]);
                    info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"]);
                    info.bgm_year = cn.ConvertNullInt(drc[0]["bgm_year"]);

                    M_BGM_StrategyDataList output = new M_BGM_StrategyDataList();
                    M_BGM_StrategyDataList act = new M_BGM_StrategyDataList();
                    M_BGM_StrategyDataList subact = new M_BGM_StrategyDataList();
                    output.bgm_strat_list_name = drc[0]["bgm_out_put_name"].ToString();
                    act.bgm_strat_list_name = drc[0]["bgm_act_name"].ToString();
                    subact.bgm_strat_list_name = drc[0]["bgm_sub_act_name"].ToString();
                    info.m_bgm_output = output;
                    info.m_bgm_activity = act;
                    info.m_bgm_subactivity = subact;

                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public M_BGM_Project GetInfo(string bgm_proj_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_BGM_Project info = new M_BGM_Project();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@bgm_proj_code", bgm_proj_code);

                strSQL = "SELECT rec.bgm_proj_seq, rec.bgm_proj_code"
                    + ", rec.bgm_proj_name, rec.bgm_proj_descr, rec.bgm_proj_date"
                    + ", rec.bgm_strat_list_seq, rec.job_site_seq, rec.bgm_year"

                    + ", strlist1.bgm_strat_list_name as bgm_sub_act_name"
                    + ", strlist2.bgm_strat_list_name as bgm_act_name"
                    + ", strlist3.bgm_strat_list_name as bgm_out_put_name"
                    + ", rec.record_status"
                    + " FROM bgm_project rec"
                    + " LEFT JOIN bgm_strategy_data_mapping strmap1 ON rec.bgm_strat_list_seq = strmap1.bgm_strat_list_seq"
                    + " LEFT JOIN bgm_strategy_data_mapping strmap2 ON strmap1.upper_bgm_strat_data_map_seq = strmap2.bgm_strat_data_map_seq"
                    + " LEFT JOIN bgm_strategy_data_mapping strmap3 ON strmap2.upper_bgm_strat_data_map_seq = strmap3.bgm_strat_data_map_seq"

                    + " LEFT JOIN bgm_strategy_data_list strlist1 ON strmap1.bgm_strat_list_seq = strlist1.bgm_strat_list_seq"
                    + " LEFT JOIN bgm_strategy_data_list strlist2 ON strmap2.bgm_strat_list_seq = strlist2.bgm_strat_list_seq"
                    + " LEFT JOIN bgm_strategy_data_list strlist3 ON strmap3.bgm_strat_list_seq = strlist3.bgm_strat_list_seq"
                    + " WHERE rec.bgm_proj_code = @bgm_proj_code"
                    + " and rec.record_status = 'A'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bgm_proj_seq = Convert.ToInt32(drc[0]["bgm_proj_seq"]);
                    info.bgm_proj_code = drc[0]["bgm_proj_code"].ToString();
                    info.bgm_proj_name = drc[0]["bgm_proj_name"].ToString();
                    info.bgm_proj_descr = drc[0]["bgm_proj_descr"].ToString();
                    info.bgm_proj_date = cn.ConvertNullDateTime(drc[0]["bgm_proj_date"]);
                    info.bgm_strategy_list_seq = cn.ConvertNullInt(drc[0]["bgm_strat_list_seq"]);
                    info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"]);
                    info.bgm_year = cn.ConvertNullInt(drc[0]["bgm_year"]);

                    M_BGM_StrategyDataList output = new M_BGM_StrategyDataList();
                    M_BGM_StrategyDataList act = new M_BGM_StrategyDataList();
                    M_BGM_StrategyDataList subact = new M_BGM_StrategyDataList();
                    output.bgm_strat_list_name = drc[0]["bgm_out_put_name"].ToString();
                    act.bgm_strat_list_name = drc[0]["bgm_act_name"].ToString();
                    subact.bgm_strat_list_name = drc[0]["bgm_sub_act_name"].ToString();
                    info.m_bgm_output = output;
                    info.m_bgm_activity = act;
                    info.m_bgm_subactivity = subact;

                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
